package com.wlz.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlz.demo.common.ResultUtil;
import com.wlz.demo.entity.Article;
import com.wlz.demo.service.IArticleService;
import com.wlz.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin_wlz
 * @since 2022-04-14
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService iArticleService;

    @Autowired
    private IUserService iUserService;

//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;
//    public static final String Article_Data_KEY = "Article_Data_KEY";

    //多个模糊查询+分页
    @GetMapping("/page")
    public IPage<Article> pageLike(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   @RequestParam(required=false) String name
    ){
        LambdaQueryWrapper<Article> lqw = new LambdaQueryWrapper<>();
        lqw.like(name!=null,Article::getName,name);

        IPage<Article> iPage = new Page<>(pageNum,pageSize);
        IPage<Article> page = iArticleService.page(iPage,lqw);
        return page;

    }


    @PostMapping("/save")
    public ResultUtil<?> save(@RequestBody Article article){


        boolean save = iArticleService.save(article);

        if (save){





            return ResultUtil.success(200,"新增成功");
        }else {
            return ResultUtil.fail(500,"新增失败");
        }
    }

    @DeleteMapping("/delete")
    public ResultUtil<?> delete(@PathParam(value = "id") Integer id){
        boolean b = iArticleService.removeById(id);
        if (b){
            List<Article> list = iArticleService.list(null);
            return ResultUtil.success(200,"删除成功",list);
        }else {
            return ResultUtil.fail(500,"删除失败");
        }

    }


    @GetMapping("/list")
    public ResultUtil<?> list(){
        List<Article> list = iArticleService.list(null);
        if (list.size()!=0){
            return ResultUtil.success(200,"查询成功",list);
        }else {
            return ResultUtil.fail(500,"查询失败");
        }
    }







    @PutMapping("/updateById")

//    @CachePut(value = "user",key = "'getAll'")
    public ResultUtil<?> updateById(@RequestBody Article article){
        boolean b = iArticleService.updateById(article);


        if (b){
            Article one = iArticleService.getById(article.getId());
            return ResultUtil.success(200,"修改成功",one);
        }else {
            return ResultUtil.fail(500,"修改失败");
        }
    }






}

